package org.misalen.web.admin.controllers;

import java.util.Date;

import org.misalen.web.database.model.mysql.pro.ProMapAdvertising;
import org.misalen.web.database.model.mysql.pro.ProMapReward;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping(value = "/pro-map-advertising-preview")
public class ProMapAdvertisingPreviewController extends BaseController {

	/**
	 * 列表页
	 * 
	 * @return 列表目标页
	 */
	@RequestMapping(value = "/details/{mapId}", method = { RequestMethod.GET })
	public ModelAndView weblist(@PathVariable String mapId) {
		ModelAndView modelAndView = new ModelAndView("/ProWebAdvertisingPreview/list2");
		modelAndView.addObject("info", proMapAdvertisingRepository.get(ProMapAdvertising.class, mapId));
		return modelAndView;
	}

	/**
	 * 列表页
	 * 
	 * @return 列表目标页
	 */
	@RequestMapping(value = "/share/{rewardId}", method = { RequestMethod.GET })
	public ModelAndView sharelist(@PathVariable String rewardId) {
		ModelAndView modelAndView = new ModelAndView("/ProWebAdvertisingPreview/list2");
		ProMapReward proMapReward = proMapRewardRepository.get(ProMapReward.class, rewardId);
		if (proMapReward.getGetTime2() == null
				&& proMapReward.getGetTime().getTime() < System.currentTimeMillis() - 24 * 60 * 60 * 1000) {
			proMapReward.setGetTime2(new Date());
			proMapRewardRepository.update(proMapReward);
			proUserRepository.updateFreezeBalance(-proMapReward.getMoney2(), proMapReward.getUserId());
			proUserRepository.updateBalance(proMapReward.getMoney2(), proMapReward.getUserId());
		}
		modelAndView.addObject("info",
				proMapAdvertisingRepository.get(ProMapAdvertising.class, proMapReward.getMapId()));
		return modelAndView;
	}
}
